package com.bw.lib_check_wdy_xin.donghua

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ImageView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bw.lib_check_wdy_xin.R

class AnimationActivity : AppCompatActivity() {
    private lateinit var animation_img_shu_wdy : ImageView
    private lateinit var animation_img_bu_wdy : ImageView
    private lateinit var animation_img_zhen_wdy : ImageView

    private var animatorSet : AnimatorSet? = null
    private var tweenAnimation : Animation? = null
    private var frameAnimation : AnimationDrawable? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_animation)

        //处理系统栏
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 初始化视图
        animation_img_shu_wdy = findViewById(R.id.animation_img_shu_wdy)
        animation_img_bu_wdy = findViewById(R.id.animation_img_bu_wdy)
        animation_img_zhen_wdy = findViewById(R.id.animation_img_zhen_wdy)

        val animation_start_wdy = findViewById<Button>(R.id.animation_start_wdy)
        val animation_stop_wdy = findViewById<Button>(R.id.animation_stop_wdy)

        // 设置帧动画资源
        try {
            animation_img_zhen_wdy.setBackgroundResource(R.drawable.frame_animation)
            frameAnimation = animation_img_zhen_wdy.background as? AnimationDrawable
        }catch (e : Exception){
            e.printStackTrace()
        }

        // 加载补间动画
        try {
            tweenAnimation = AnimationUtils.loadAnimation(this,R.anim.rotate_animation)
        }catch (e : Exception){
            e.printStackTrace()
        }

        // 设置按钮点击事件
        animation_start_wdy.setOnClickListener { startAllAnimations() }
        animation_stop_wdy.setOnClickListener { stopAllAnimations() }
    }

    private fun startAllAnimations(){
        try {
            // 开始属性动画
            val scaleX = ObjectAnimator.ofFloat(animation_img_shu_wdy,"scaleX",1f,1.5f)
            val scaleY = ObjectAnimator.ofFloat(animation_img_shu_wdy,"scaleY",1f,1.5f)
            val rotation = ObjectAnimator.ofFloat(animation_img_shu_wdy,"rotation",0f,360f)

            animatorSet = AnimatorSet().apply {
                playTogether(scaleX,scaleY,rotation)
                duration = 2000
                start()
            }

            // 开始补间动画
            tweenAnimation?.let { animation_img_bu_wdy.startAnimation(it) }

            // 开始帧动画
            frameAnimation?.start()
        }catch (e : Exception){
            e.printStackTrace()
        }
    }
    private fun stopAllAnimations(){
        try {
            // 停止属性动画
            animatorSet?.cancel()
            animation_img_shu_wdy.clearAnimation()
            animation_img_shu_wdy.scaleX = 1f
            animation_img_shu_wdy.scaleY = 1f
            animation_img_shu_wdy.rotation = 0f

            // 停止补间动画
            animation_img_bu_wdy.clearAnimation()

            // 停止帧动画
            frameAnimation?.stop()
        }catch (e : Exception){
            e.printStackTrace()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        stopAllAnimations()
    }
}